package com.chinasoft04.mall.service.impl;

import com.chinasoft04.mall.dto.Attr;
import com.chinasoft04.mall.dto.Good;
import com.chinasoft04.mall.dto.Goods;
import com.chinasoft04.mall.dto.Picture;
import com.chinasoft04.mall.mapper.GoodsMapper;
import com.chinasoft04.mall.service.GoodsService;
import com.chinasoft04.mall.utils.Magick;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import org.im4java.core.IM4JavaException;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
@AllArgsConstructor
public class GoodsServiceImpl implements GoodsService {
    GoodsMapper goodsMapper;


    @Override
    public List<Goods> getGoods(String query, Integer pagenum, Integer pagesize) {

        StringBuffer sb = new StringBuffer(query);
        sb.insert(0,"%");
        sb.append("%");
        //szk
        System.out.println("查询字符："+sb);
        String reQuery = new String(sb);
        PageHelper.startPage(pagenum,pagesize);
        List<Goods> goodsList = goodsMapper.selectAllGoods(reQuery);

        return goodsList;
    }

    @Override
    public Good getGood(Integer id) {
        Good good = goodsMapper.selectGoodById(id);
        List<Picture> pics = goodsMapper.selectPics(id);
        good.setPics(pics);
        List<Attr> attrs = goodsMapper.selectAttrs(id);
        good.setAttrs(attrs);
//        good.setAttrs(goodsMapper.selectAttrs(id));
//        good.setPics(goodsMapper.selectPics(id));




        return good;
    }

    @Override
    public boolean updateAttr(Attr attr) {


        return false;
    }

    @Override
    public boolean updateGood(Integer id, Good good) {
        int flag = goodsMapper.updateGoodInfo(id,good);
        if (flag!=0){


            return true;
        }


        return false;
    }

    @Override
    public boolean deleteGood(Integer id) {
        int flag = goodsMapper.deleteGood(id);
        if(flag!=0){
            return true;
        }


        return false;
    }

    @Override
    public Good addGood(Good good,String realPath) throws IOException, InterruptedException, IM4JavaException {
        //添加商品模块
        Magick magick = new Magick();


        File smaDir = new File(realPath+"smaImg");
        File midDir = new File(realPath+"midImg");
        File bigDir = new File(realPath+"bigImg");
        smaDir.mkdir();
        midDir.mkdir();
        bigDir.mkdir();



        //图片处理
        for(Picture pic : good.getPics()){

            //首先从good对象中取出临时图片,并获取图片名字
            String filename = pic.getPic().substring(12);
            //将小中大的路径都存到Picture对象中;
            pic.setPics_sma("smaImg\\"+filename);
            pic.setPics_mid("midImg\\"+filename);
            pic.setPics_big("bigImg\\"+filename);

            magick.reSize(pic,realPath);

            pic.setPics_sma(realPath+pic.getPics_sma());
            pic.setPics_mid(realPath+pic.getPics_mid());
            pic.setPics_big(realPath+pic.getPics_big());
        }
        System.out.println("图片已处理");


        Date date = new Date();
        good.setUpd_time(date.getTime());
        good.setAdd_time(date.getTime());
        goodsMapper.addGood(good,good.getPics().get(0).getPics_sma());
        return null;
    }

}
